#!/usr/bin/env node
/* Usage: node server.js [port] */
const http = require('http')

const port = process.argv[2] || 3000

http.createServer(function (request, response) {
  console.log('url', request.url)
  console.log('method', request.method)
  console.log('headers', request.headers)

  let data = ''

  request.on('data', function (chunk) {
    data += chunk
  })

  request.on('end', function () {
    console.log('----------body----------')
    console.log(data)
    response.end('request received.')
  })
}).listen(port, err => {
  if (err) {
    console.log(`Something bad happened: ${err}.`)
  } else {
    console.log(`Server is listening on ${port}.`)
  }
})
